<?php
class GoodsAction extends Action{
	public function index(){
        $Data = M('goods'); // 实例化Data数据对象
        import('ORG.Util.Page');// 导入分页类
        $count      = $Data->where($map)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,5);// 实例化分页类 传入总记录数
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $list = $Data->where($map)->order('id')->page($nowPage.','.$Page->listRows)->select();
        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('list',$list);// 赋值数据集
        $this->display(); // 输出模板
	}
	public function add(){
		$Platform = M('platform');
		$this->platform = $Platform->select();
		$this->display();
	}
	public function insert(){
		$Goods = M('goods');
		$data['title'] = $_POST['title'];
		//$data['sku'] = $_POST['sku'];
		$data['purchasing_price'] = $_POST['purchasing_price'];
		$data['stock'] = $_POST['stock'];
		$data['weight'] = $_POST['weight'];
		if($_POST['platform']){
			$data['platform'] = implode('|', $_POST['platform']);
		}else{
			$data['platform'] = '';
		}
		$data['content'] = $_POST['content'];
		$data['date'] = date('Y-m-j');
		$Goods->add($data);
		$id = mysql_insert_id();
		$data['sku']=$id+10000;
		import('ORG.Net.UploadFile');
		$upload = new UploadFile();// 实例化上传类
		$upload->maxSize  = 3145728 ;// 设置附件上传大小
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath =  'Uploads/Goods/';// 设置附件上传目录
		if(!$upload->upload()) {// 上传错误提示错误信息
			$this->error($upload->getErrorMsg());
		}else{// 上传成功
			$this->success('上传成功！');
		}
		$info = $upload->getUploadFileInfo();
		$data['picture'] = $info[0]['savename'];
		$Goods->where("id=$id")->save($data);
	}
	public function edit(){
		$Goods = M('goods');
		$id = $_GET['id'];
		$this->data = $Goods->where("id=$id")->find();
		$this->display();
	}
}
?>